package interfaces.clients.rightscontrol;

import interfaces.clients.storage.NoDiffsRepository;
import interfaces.clients.storage.Repository;
import interfaces.clients.storage.StorageFileNotFoundException;
import interfaces.clients.storage.StorageIOException;

import java.util.List;

/**
 * @author Mihail A. Muhin
 */
public class RightsControlSystem implements RightsControl {
	private static RightsControlSystem instance = null;

	private static Repository repos = NoDiffsRepository.getInstance();

	private RightsControlSystem() {

	}

	public static RightsControl getInstance() {
		if(instance == null) {
			instance = new RightsControlSystem();
		}
		return instance;
	}


	public String getFile(String filename, int version) {
		//TODO: make something appropriate with this exception(maybe smth like showExpection)
		try {
			return repos.getFile(filename, version);
		} catch (StorageFileNotFoundException e) {
			//e.printStackTrace();
		} catch (StorageIOException e) {
			//
		}
		return null;
	}

	public void putFile(String filename, String contents) {
		try {
			repos.putFile(filename, contents);
		} catch (StorageIOException e) {
			System.out.println(e.getMessage());
		} catch (StorageFileNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}

	public int getLastVersion(String filename) {
		return repos.getLastVersion(filename);
	}

	public List getFiles() {
		return repos.getFiles();
	}
}
